<template>
  <div class="avue-top">
    <div class="top-bar__left">
      <div
        v-if="showCollapse"
        :class="[{ 'avue-breadcrumb--active': isCollapse }]"
        class="avue-breadcrumb"
      >
        <i class="icon-navicon" @click="setCollapse"></i>
      </div>
    </div>
    <!-- 顶部菜单 -->
    <div style="margin-left: 20px">
      <el-menu mode="horizontal" text-color="#333" :default-active="activeMenu">
        <template v-for="(item, index) in menuList">
          <el-menu-item
            :key="index"
            :index="item.path"
            @click.native="openMenu(item, index)"
          >
            <template slot="title">
              <i :class="item.icon"></i>
              <span>{{ item.label }}</span>
            </template>
          </el-menu-item>
        </template>
      </el-menu>
    </div>
    <!--  -->
    <div class="top-bar__title">
      <div v-if="showMenu" class="top-bar__item top-bar__item--show">
        <top-menu></top-menu>
      </div>
    </div>
    <div class="top-bar__right">
      <el-tooltip
        v-if="showLock"
        content="锁屏"
        effect="dark"
        placement="bottom"
      >
      </el-tooltip>
      <el-tooltip
        v-if="showFullScren"
        :content="isFullScreen ? '退出全屏' : '全屏'"
        effect="dark"
        placement="bottom"
      >
        <div class="top-bar__item">
          <i
            :class="isFullScreen ? 'icon-zuixiaohua' : 'icon-quanpingzuidahua'"
            @click="handleScreen"
          ></i>
        </div>
      </el-tooltip>
      <el-dropdown>
        <span class="el-dropdown-link">
          {{ userInfo.username }}
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item>
            <router-link to="/">首页</router-link>
          </el-dropdown-item>
          <el-dropdown-item>
            <router-link to="/info/index">个人信息</router-link>
          </el-dropdown-item>
          <el-dropdown-item divided @click.native="logout"
            >退出系统
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>
<script>
import { mapGetters, mapState } from "vuex";
import { fullscreenToggel, listenfullscreen } from "@/util/util";
import topMenu from "./top-menu";
import { getStore, removeStore, setStore } from "@/util/store";

export default {
  components: {
    topMenu,
  },
  name: "top",
  data() {
    return {
      menuList: [],
      activeMenu: "",
    };
  },
  watch: {
    "$store.state.user.path": {
      handler(newPath) {
        // 当 path 为空时，重置 activeMenu
        if (!newPath || !newPath.path) {
          this.activeMenu = "";
          return;
        }

        // 查找匹配的顶部菜单项
        const topMenuItem = this.menuList.find((item) => {
          return (
            item.path === newPath.path ||
            (item.children &&
              item.children.some((child) => child.path === newPath.path))
          );
        });

        if (topMenuItem) {
          this.activeMenu = topMenuItem.path;
        }
      },
    },
  },
  filters: {},
  created() {},
  mounted() {
    listenfullscreen(this.setScreen);
    this.$store.dispatch("GetMenu", { type: true, id: -1 }).then((res) => {
      this.menuList = res;
      // 从 sessionStorage 恢复选中的菜单
      const path = getStore({ name: "activeMenu" });
      if (path) {
        this.activeMenu = path;
        this.$store.dispatch("setPath", { path });
      }
    });
  },
  computed: {
    ...mapState({
      showLock: (state) => state.common.showLock,
      showFullScren: (state) => state.common.showFullScren,
      showCollapse: (state) => state.common.showCollapse,
      showMenu: (state) => state.common.showMenu,
    }),
    ...mapGetters([
      "userInfo",
      "isFullScreen",
      "tagWel",
      "tagList",
      "isCollapse",
      "tag",
      "logsLen",
      "logsFlag",
      "menu",
    ]),
  },
  methods: {
    handleScreen() {
      fullscreenToggel();
    },
    setCollapse() {
      this.$store.commit("SET_COLLAPSE");
    },
    setScreen() {
      this.$store.commit("SET_FULLSCREN");
    },
    logout() {
      this.$confirm("是否退出系统, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$store.dispatch("LogOut").then(() => {
          localStorage.clear();
          sessionStorage.clear();
          // this.$router.push({ path: "/login" });
          location.reload();
        });
      });
    },
    openMenu(e, index) {
      // 保存顶部菜单状态
      setStore({
        name: "activeMenu",
        content: e.path,
        type: "session",
      });
      // 保存侧边栏菜单状态
      setStore({
        name: "sidebarMenu",
        content: e.children,
        type: "session",
      });
      this.$store.dispatch("setPath", e.path);
      this.$store.dispatch("setNewMenuList", e.children);
    },
  },
};
</script>

<style lang="scss" scoped></style>
